package dao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import pojo.Admin;
import pojo.Bed;
import pojo.ChangeRoom;
import pojo.CheckInRecord;
import service.BedServiceImpl;
import utils.JDBCUtils;
import utils.SystemEnum;

import java.sql.*;
import java.util.ArrayList;

public class CheckInRecordDaoImpl implements CheckInRecordDao{

    //入住信息
    private Log log = LogFactory.getLog(getClass());

    @Override
    public ArrayList<CheckInRecord> findAll() {
        log.info(SystemEnum.BEGIN);
        ArrayList<CheckInRecord> list = new ArrayList<CheckInRecord>();
        Connection con = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {
            con = JDBCUtils.getConnection();
            String sql = "SELECT * FROM check_in_record ";
            statement = con.prepareStatement(sql);
            rs = statement.executeQuery();
            while(rs.next()){
                CheckInRecord checkInRecord = new CheckInRecord(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getInt(4),rs.getDate(5),rs.getDate(6),rs.getBigDecimal(7),rs.getBigDecimal(8),rs.getBoolean(9),rs.getBigDecimal(10));
                list.add(checkInRecord);
            }
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
        }finally {
            JDBCUtils.close(con,statement,rs);
            log.info(SystemEnum.END);
        }
        return list;
    }

    @Override
    public CheckInRecord findById(Integer check_in_id) {
        log.info(SystemEnum.BEGIN);
        CheckInRecord checkInRecord = new CheckInRecord();
        Connection con = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {
            con = JDBCUtils.getConnection();
            String sql = "SELECT * FROM check_in_record WHERE check_in_id = ?";
            statement = con.prepareStatement(sql);
            statement.setInt(1,check_in_id);
            rs = statement.executeQuery();
            while(rs.next()){
                checkInRecord.setCheck_in_id(rs.getInt(1));
                checkInRecord.setStuId(rs.getInt(2));
                checkInRecord.setDormId(rs.getInt(3));
                checkInRecord.setBedId(rs.getInt(4));
                checkInRecord.setCherk_in_time(rs.getDate(5));
                checkInRecord.setPlan_cherk_out_time(rs.getDate(6));
                checkInRecord.setPayment(rs.getBigDecimal(7));
                checkInRecord.setActual_payment(rs.getBigDecimal(8));
                checkInRecord.setTakenKey(rs.getBoolean(9));
                checkInRecord.setPayedCash(rs.getBigDecimal(10));
            }
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
        }finally {
            JDBCUtils.close(con,statement,rs);
            log.info(SystemEnum.END);
        }
        return checkInRecord;
    }

    @Override
    public int insert(Connection con,CheckInRecord checkInRecord) {
        log.info(SystemEnum.BEGIN);

        PreparedStatement statement = null;
        PreparedStatement statement2 = null;
        ResultSet rs = null;
        int result = 0;
        try {


            //判断床位是否为空
            String sqlBed = "SELECT isUsed FROM bed WHERE dormId = ? AND bedId = ?";
            statement2 = con.prepareStatement(sqlBed);
            statement2.setInt(1,checkInRecord.getDormId());
            statement2.setInt(2,checkInRecord.getBedId());
            rs = statement2.executeQuery();
            while (rs.next()){
                if(rs.getBoolean(1)){
                    //已经有人住
                    throw new Exception(SystemEnum.ERROR_REPEAT.toString());
                }
            }
            //添加入住信息
            String sql = "INSERT INTO check_in_record VALUES (?,?,?,?,?,?,?,?,?,?) ";
            statement = con.prepareStatement(sql);
            statement.setInt(1,checkInRecord.getCheck_in_id());
            statement.setInt(2,checkInRecord.getStuId());
            statement.setInt(3,checkInRecord.getDormId());
            statement.setInt(4,checkInRecord.getBedId());
            statement.setDate(5, new java.sql.Date(checkInRecord.getCherk_in_time().getTime()));
            statement.setDate(6, new java.sql.Date(checkInRecord.getPlan_cherk_out_time().getTime()));
            statement.setBigDecimal(7,checkInRecord.getPayment());
            statement.setBigDecimal(8,checkInRecord.getActual_payment());
            statement.setBoolean(9,checkInRecord.isTakenKey());
            statement.setBigDecimal(10,checkInRecord.getPayedCash());
            result += statement.executeUpdate();
            log.info("增加入住信息"+SystemEnum.SUCCESS);

            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            try {
                con.rollback();
                log.info("新增入住信息异常，执行回滚操作");
            } catch (SQLException ex) {
                ex.printStackTrace();
            }

        }
        return result;
    }

    @Override
    public int update(CheckInRecord checkInRecord) {
        log.info(SystemEnum.BEGIN);
        Connection con = null;
        PreparedStatement statement = null;
        PreparedStatement statement2 = null;
        int result = 0;
        try {
            con = JDBCUtils.getConnection();
            String sql = "UPDATE check_in_record SET stuId = ?,check_in_time = ?,plan_cherk_out_time = ?,payment = ?,actual payment = ?,isTakenKey = ?,payedCash = ? WHERE check_in_id = ?";
            statement = con.prepareStatement(sql);
            statement.setInt(1,checkInRecord.getStuId());
            statement.setDate(2, (Date) checkInRecord.getCherk_in_time());
            statement.setDate(3, (Date) checkInRecord.getPlan_cherk_out_time());
            statement.setBigDecimal(4,checkInRecord.getPayment());
            statement.setBigDecimal(5,checkInRecord.getActual_payment());
            statement.setBoolean(6,checkInRecord.isTakenKey());
            statement.setBigDecimal(7,checkInRecord.getPayedCash());
            statement.setInt(8,checkInRecord.getCheck_in_id());
            result = statement.executeUpdate();
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
        }finally {
            JDBCUtils.close(con,statement);
            log.info(SystemEnum.END);
        }
        return result;
    }

    @Override
    public int delete(CheckInRecord checkInRecord) {
        log.info(SystemEnum.BEGIN);
        Connection con = null;
        PreparedStatement statement = null;
        PreparedStatement statement2 = null;
        int result = 0;
        try {
            con = JDBCUtils.getConnection();
            con.setAutoCommit(false);
            //修改床铺状态
            BedServiceImpl bedService = new BedServiceImpl();
            bedService.update(bedService.findById(checkInRecord.getDormId(),checkInRecord.getBedId()));

            //删除入住信息
            String sql = "DELETE FROM check_in_record  WHERE check_in_id = ? ";
            statement = con.prepareStatement(sql);
            statement.setInt(1,checkInRecord.getCheck_in_id());
            result = statement.executeUpdate();
            log.info("删除入住信息\t"+SystemEnum.SUCCESS);
            con.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                log.info(SystemEnum.ERROR_INEFFECTIVE+"\t正在回滚");
                con.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
                log.info("回滚\t"+SystemEnum.ERROR_INEFFECTIVE);
            }
        }finally {
            log.info(SystemEnum.END);
            try {
                con.setAutoCommit(true);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            JDBCUtils.close(con,statement);
        }
        return result;
    }
}
